import java.util.*;
import saito.objloader.*;
import ddf.minim.*;
import TUIO.*;

int _screenWidth;
int _screenHeight;
List<ObjectModel> _objects;
Minim _minim;
TUIOClient _client;

void setup() {
  this._minim = new Minim(this);
  this._screenWidth = screen.width;
  this._screenHeight = screen.height;
  this._objects = new ArrayList<ObjectModel>();
  size(_screenWidth, _screenHeight, P3D);
  this._client = new TUIOClient(this, this._screenWidth, this._screenHeight);
  createObjects();
//  noCursor();
}

void draw() { 
  background(0);
  for (final ObjectModel o: this._objects) {
    o.update();
    o.draw();
  }
  checkCollision();
}

void createObjects() {
  this._objects.add(new BallModel(50, new PVector (this._screenWidth/2, this._screenHeight/2, -500), new PVector (1, 0, 0), 10, color(255, 0, 0), 1));
  this._objects.add(new SceneModel(new PVector(this._screenWidth/2, this._screenHeight/2, -500), new PVector(this._screenWidth - 400, this._screenHeight, 998), 1));
  this._objects.add(new ControlerModel(new PVector(120,200,0), true));
  this._objects.add(new ControlerModel(new PVector(120,this._screenHeight - 200,0), true));
  this._objects.add(new ControlerModel(new PVector(this._screenWidth - 120,200,0), false));
  this._objects.add(new ControlerModel(new PVector(this._screenWidth - 120,this._screenHeight - 200,0), false));
}

public void rebound(int wall) {
  PVector newDir = ((BallModel)this._objects.get(0)).getDirection();

  switch(wall) {
  case 1:
    newDir.x*=-1;     
    break;
  case 2:
    newDir.y*=-1;
    break;
  case 3 :
    newDir.z*=-1;
    break;
  }
  ((BallModel)this._objects.get(0)).setDirection(newDir);
}

public void checkCollision() {
  //Bord terrain
  if (this._objects.get(0)._boundingbox.getCornerTopLeft().x < this._objects.get(1)._boundingbox.getCornerTopLeft().x) {
    rebound(1);
  }
  if (this._objects.get(0)._boundingbox.getCornerBottomRight().x > this._objects.get(1)._boundingbox.getCornerBottomRight().x-this._objects.get(0).getSize().x) {
    rebound(1);
  }
  if (this._objects.get(0)._boundingbox.getCornerTopLeft().y < this._objects.get(1)._boundingbox.getCornerTopLeft().y) {
    rebound(2);
  }
  if (this._objects.get(0)._boundingbox.getCornerBottomRight().y > this._objects.get(1)._boundingbox.getCornerBottomRight().y-this._objects.get(0).getSize().y) {
    rebound(2);
  }
  if (this._objects.get(0)._boundingbox.getCornerTopLeft().z < this._objects.get(1)._boundingbox.getCornerTopLeft().z) {
    rebound(3);
  }
  if (this._objects.get(0)._boundingbox.getCornerBottomRight().z > this._objects.get(1)._boundingbox.getCornerBottomRight().z-this._objects.get(0).getSize().z) {
    rebound(3);
  }
   //Portail
  PVector tmpBallCenter = this._objects.get(0)._boundingbox.getCenter();
  if (this._objects.get(2)._boundingbox.inBoundingBox(tmpBallCenter)) 
    passPortal(2);
  if (this._objects.get(3)._boundingbox.inBoundingBox(tmpBallCenter)) 
    passPortal(3);
  if (this._objects.get(4)._boundingbox.inBoundingBox(tmpBallCenter)) 
    passPortal(4);
  if (this._objects.get(5)._boundingbox.inBoundingBox(tmpBallCenter))
    passPortal(5);
}

public void passPortal(int idPortalIn) {
  switch(idPortalIn) {
  case 2: 
    this._objects.get(0).setPosition(this._objects.get(3)._boundingbox.getCenter()); 
    break;
  case 3: 
    this._objects.get(0).setPosition(this._objects.get(2)._boundingbox.getCenter()); 
    break;
  case 4: 
    this._objects.get(0).setPosition(this._objects.get(5)._boundingbox.getCenter()); 
    break;
  case 5: 
    this._objects.get(0).setPosition(this._objects.get(4)._boundingbox.getCenter()); 
    break;
  }
}

